
using System;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace OnePiece.Core.ModelComponent
{
    [DataContract]
    public abstract class ModelComponentBase
    {
 

        #region Constructor
        public ModelComponentBase()
            : this("ModelComponentBase")
        {

        }

        public ModelComponentBase(string modelName)
        {
            this.ModelName = modelName;
        } 
        #endregion

        #region Common Properties

        /// <summary>
        /// The EntityId of the component
        /// </summary>
        [DataMember]
        public abstract Guid EntityId { get; set; }

        /// <summary>
        /// The rowversion of component, used for the concurrency logics
        /// </summary>
        [DataMember]
        public virtual byte[] RowVersion { get; set; }

        /// <summary>
        /// The actual values are not really deleted in the reposity is just mark as deleted
        /// </summary>
        [DataMember]
        public virtual bool IsDeleted { get; set; }


        private bool _isNew = true;
        [DataMember]
        public virtual bool IsNew
        {
            get { return _isNew; }
            set { _isNew = value;}
        }

        [DataMember]
        public virtual bool IsDirty { get; set; }

        private bool _isValid = true;
        [DataMember]
        public virtual bool IsValid
        {
            get { return _isValid; }
            set { _isValid = value; }
        }

        [DataMember]
        public virtual string Remarks { get; set; }

        #endregion

        #region Helpers
        public virtual void SetDirty()
        {
            IsDirty = true;                     
        }

        public string ModelName { get; set; }

        #endregion

        #region Event Notifications

        /// <summary>
        /// For event notification
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                //     VerifyCalledOnUIThread();
                _propertyChangedEvent += value;
            }
            remove
            {
                //     VerifyCalledOnUIThread();
                _propertyChangedEvent -= value;
            }
        }

        /// <summary>
        /// Raises an property change event.
        /// </summary>
        /// <param name="propertyName"></param>
        protected void SendPropertyChanged(string propertyName)
        {
            //  VerifyCalledOnUIThread();
            if (_propertyChangedEvent != null)
            {
                _propertyChangedEvent(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Event declaration
        /// </summary>
        private PropertyChangedEventHandler _propertyChangedEvent; 
        #endregion

    }
}